<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/1/17
 * Time: 13:45
 */

namespace app\admin\controller;

use think\Db;
use \app\admin\model\Product as ProductModel;
use \app\admin\model\Resource as ResourceModel;

class Wallet extends Main
{

    /******************************* 充值理由 ****************************/
    /**
     * 钱包充值理由
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function cateRecharge()
    {
        $data = Db::name('wallet_recharge_category')
            ->where('company_id', isession('company_id'))
            ->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('cateRecharge');
    }

    /**
     * 添加钱包充值理由
     * @return mixed
     */
    public function cateRechargeAdd()
    {
        if (post()) {
            $data['reason'] = spost('reason');
            $data['company_id'] = isession('company_id');
            $data['timestamp'] = time();
            Db::name('wallet_recharge_category')->insert($data);
            $this->success('添加成功');
        } else {
            return $this->fetch('cateRechargeAdd');
        }
    }

    /**
     * 编辑钱包充值理由
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function cateRechargeEdit($id)
    {
        if (post()) {
            DB::name('wallet_recharge_category')->where('id', post('id'))->update(['reason' => spost('reason')]);
            $this->success("更新成功");
        } else {
            $info = DB::name('wallet_recharge_category')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('cateRechargeEdit');
        }
    }

    /**
     * 删除钱包充值理由
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function cateRechargeDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where['id'] = ['in', $data['id']];
        } else {
            $where['id'] = $data['id'];
        }
        Db::name('wallet_recharge_category')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 获取充钱理由
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getRechargeList()
    {
        $data = Db::name('wallet_recharge_category')
            ->where('company_id', isession('company_id'))
            ->select();
        return $data;
    }

    /******************************* 支出理由 ****************************/
    /**
     * 钱包支出理由
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function catePayment()
    {
        $data = Db::name('wallet_payment_category')
            ->where('company_id', isession('company_id'))
            ->paginate(15);
        $this->assign('list', $data);
        return $this->fetch('catePayment');
    }

    /**
     * 添加钱包支出理由
     * @return mixed
     */
    public function catePaymentAdd()
    {
        if (post()) {
            $data['reason'] = spost('reason');
            $data['company_id'] = isession('company_id');
            $data['timestamp'] = time();
            Db::name('wallet_payment_category')->insert($data);
            $this->success('添加成功');
        } else {
            return $this->fetch('catePaymentAdd');
        }
    }

    /**
     * 编辑钱包支出理由
     * @param $id
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function catePaymentEdit($id)
    {
        if (post()) {
            DB::name('wallet_payment_category')->where('id', post('id'))->update(['reason' => spost('reason')]);
            $this->success("更新成功");
        } else {
            $info = DB::name('wallet_payment_category')->where('id', $id)->find();
            $this->assign("info", $info);
            return $this->fetch('catePaymentEdit');
        }
    }

    /**
     * 删除钱包支出理由
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function catePaymentDel()
    {
        $data = post();
        if (checkArr($data['id'])) {
            $where = "id in (" . join(",", $data['id']) . ")";
        } else {
            $where = "id =" . $data['id'];
        }
        Db::name('wallet_payment_category')->where($where)->delete();
        $this->success("删除成功");
    }

    /**
     * 获取支出理由
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPaymentList()
    {
        $data = Db::name('wallet_payment_category')
            ->where('company_id', isession('company_id'))
            ->select();
        return $data;
    }

    /******************************* 钱包列表 ****************************/
    /**
     * 钱包列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function wallet()
    {
        $wallets = [];
        $companys = [];
        if (isession('type') == 2) {//子公司
            $wallet = DB::name('wallet')->where('company_id', isession('company_id'))->select();
            $company = DB::name('company')->where('id', isession('company_id'))->find();
            checkArr($wallet) && $wallet = arrayCombine($wallet, 'purpose');
        } else {
            $wallet = DB::name('wallet')->where('company_id', isession('company_id'))->select();
            $company['name'] = '总公司';
            $company['id'] = 0;
            $company['type'] = 1;
            checkArr($wallet) && $wallet = arrayCombine($wallet, 'purpose');

            $company_id = $this->getSubsidiary();
            $where['company_id'] = ['in', join(',', $company_id)];
            $where['purpose'] = 2;
            $where['type'] = 2;
            $wallets = DB::name('wallet')->where($where)->select();
            checkArr($wallets) && $wallets = arrayCombine($wallets, 'company_id');
            $companys = DB::name('company')->where(['id' => ['in', $company_id]])->select();
        }

        $this->assign('wallet', $wallet);
        $this->assign('company', $company);

        $this->assign('wallets', $wallets);
        $this->assign('companys', $companys);

        return $this->fetch('list');
    }

    /**
     * 总公司获取旗下子公司
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getSubsidiary()
    {
        //获取子公司id
        $where['flag'] = 1;
        $where['type'] = 2;
        $where['status'] = 1;
        $company_ids = Db::name('user')
            ->distinct(true)
            ->field('company_id')
            ->where($where)
            ->select();
        $company_id = checkArr($company_ids) ? array_column($company_ids, 'company_id') : [];
        return $company_id;
    }

    /**
     * 充值钱包
     */
    public function recharge()
    {
        if (post()) {
            $data = post();
            if ($data['purpose'] == 1) {//自用
                $this->realRecharge($data);
            } else {//对账
                if (isession('type') == 1) {
                    $this->realRecharge($data);
                } else {
                    $this->rechargeLog($data, 1);
                }
            }
            $this->success("充值成功");
        } else {
            $this->assign('rechar', $this->getRechargeList());//获取充钱理由
            $this->assign('date', date("Y-m-d", time()));
            return $this->fetch('recharge');
        }
    }

    /**
     * 支出钱包
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function expense()
    {
        if (post()) {
            $data = post();
            $this->realExpense($data);
            $this->success("支出成功");
        } else {
            $this->assign('expense', $this->getPaymentList());//获取充钱理由
            $this->assign('date', date("Y-m-d", time()));
            return $this->fetch('expense');
        }
    }

    /**
     * 上传凭证
     */
    public function upload_voucher()
    {
        $dir = '/files/' . isession('user_id') . '/wallet/' . date("Y-m-d", time()) . "/";
        $res = $this->imgUploads($_FILES['voucher'], $dir, ['jpg', 'jpeg', 'png']);
        echo json_encode($res);
        exit;
    }

    /**
     * 即时充值
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function realRecharge($data = [])
    {
        //检测充值公司钱包是否存在
        $where['company_id'] = $data['company_id'];
        $where['purpose'] = $data['purpose'];
        $wallet = DB::name('wallet')->where($where)->find();
        if (checkArr($wallet)) {//存在，更新钱包
            $sql = "UPDATE ean_wallet SET money = (money+" . $data['money'] . ") WHERE id =" . $wallet['id'];
            Db::execute($sql);
            $wallet_id = $wallet['id'];
        } else {//不存在，添加钱包
            $insertData['money'] = $data['money'];
            $insertData['company_id'] = $data['company_id'];
            $insertData['type'] = $data['type'];
            $insertData['purpose'] = $data['purpose'];
            $insertData['timestamp'] = time();
            DB::name('wallet')->insert($insertData);
            $wallet_id = Db::name('wallet')->getLastInsID();
        }
        $this->rechargeLog($data, 2, $wallet_id);
        return true;
    }

    /**
     * 即时支出,记录日志
     * @param array $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function realExpense($data = [])
    {
        $res = $this->checkMoneyEnough($data['money'], $data['id']);
        if ($res['res'] == false) {
            $this->error($res['msg']);
        }
        $sql = "UPDATE ean_wallet SET money = (money-" . $data['money'] . ") WHERE id =" . $data['id'];
        Db::execute($sql);
        //存在上传凭证
        if (isset($data['name']) && count($data['name']) > 0) {
            $productModel = new ProductModel();
            foreach ($data['name'] as $key => $value) {
                $resource_ids[] = $productModel->insertResource($value, $data['path'][$key], $data['size'][$key], $data['postfix'][$key], $data['original'][$key], '', 'wallet');
            }
            $resource_id = join(',', $resource_ids);
        }

        $log['wallet_id'] = $data['id'];
        $log['money'] = $data['money'];
        $log['company_id'] = $data['company_id'];
        $log['type'] = $data['type'];
        $log['purpose'] = $data['purpose'];
        $log['operate'] = 2;
        $log['operate_company_id'] = isession('company_id');
        $log['operate_date'] = strtotime($data['execdate']);
        $log['reason_id'] = $data['expense_id'];
        $log['reason'] = $data['expense'];
        $log['resource_id'] = isset($resource_id) ? $resource_id : '';
        $log['remark'] = $data['remark'];
        $log['project'] = 2;
        $log['audit'] = 2;
        $log['operate_audit'] = 3;
        $log['operate_uid'] = isession('user_id');
        $log['operate_name'] = ssession('username');
        $log['operate_truename'] = ssession('truename');
        $log['timestamp'] = time();
        DB::name('wallet_log')->insert($log);
        return true;
    }

    /**
     * 记录钱包日志
     * @param array $data
     * @param int $audit
     * @param string $wallet_id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function rechargeLog($data = [], $audit = 1, $wallet_id = '')
    {
        if (empty($wallet_id)) {//获取钱包id
            $where['company_id'] = $data['company_id'];
            $where['purpose'] = $data['purpose'];
            $wallet = DB::name('wallet')->where($where)->find();
            if (!checkArr($wallet)) {//不存在钱包，创建空钱包
                $insertData['money'] = 0;
                $insertData['company_id'] = $data['company_id'];
                $insertData['type'] = $data['type'];
                $insertData['purpose'] = $data['purpose'];
                $insertData['timestamp'] = time();
                DB::name('wallet')->insert($insertData);
                $wallet_id = Db::name('wallet')->getLastInsID();
            } else {
                $wallet_id = $wallet['id'];
            }
        }

        $resource_id = '';
        //存在上传凭证
        if (isset($data['name']) && count($data['name']) > 0) {
            $productModel = new ProductModel();
            foreach ($data['name'] as $key => $value) {
                $resource_ids[] = $productModel->insertResource($value, $data['path'][$key], $data['size'][$key], $data['postfix'][$key], $data['original'][$key], '', 'wallet');
            }
            $resource_id = join(',', $resource_ids);
        }


        $log['wallet_id'] = $wallet_id;
        $log['money'] = $data['money'];
        $log['company_id'] = $data['company_id'];
        $log['type'] = $data['type'];
        $log['purpose'] = $data['purpose'];
        $log['operate'] = 1;
        $log['operate_company_id'] = isession('company_id');
        $log['operate_date'] = strtotime($data['execdate']);
        $log['reason_id'] = $data['rechar_id'];
        $log['reason'] = $data['rechar'];
        $log['resource_id'] = $resource_id;
        $log['remark'] = $data['remark'];
        $log['project'] = 1;
        $log['audit'] = $audit;
        $log['operate_audit'] = $audit == 1 ? 1 : 3;
        $log['operate_uid'] = isession('user_id');
        $log['operate_name'] = ssession('username');
        $log['operate_truename'] = ssession('truename');
        $log['timestamp'] = time();
        DB::name('wallet_log')->insert($log);
    }

    /**
     * 账单
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function bill()
    {
        //检测公司钱包是否存在
        $where['company_id'] = iget('company_id');
        $where['purpose'] = iget('purpose');
        $wallet = DB::name('wallet')->where($where)->find();

        $data = [];
        if (checkArr($wallet)) {
            $where2 = [];
            $where1['wallet_id'] = $wallet['id'];
            get('start') && $where1['timestamp'] = ['>=', strtotime(get('start') . " 00:00:00")];
            get('end') && $where2['timestamp'] = ['<=', strtotime(get('end') . " 23:59:59")];
            iget('audit') && $where1['audit'] = iget('audit');
            $data = Db::name('wallet_log')
                ->where($where1)
                ->where($where2)
                ->order('timestamp desc')
                ->paginate(15, false, ['query' =>
                    [
                        'audit' => iget('audit'),
                        'purpose' => iget('purpose'),
                        'start' => sget('start'),
                        'end' => sget('end'),
                        'type' => iget('type'),
                        'company_id' => iget('company_id')
                    ]]);
        }
        $this->assign('list', $data);
        return $this->fetch('bill');
    }

    /**
     * 账单详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function billInfo($id)
    {
        $info = DB::name('wallet_log')->where('id', $id)->find();
        if (checkArr($info) && !empty($info['resource_id'])) {
            $ResourceModel = new ResourceModel();
            $info['resour'] = $ResourceModel->getResourceByIds($info['resource_id'], $info['operate_company_id']);
        }
        $this->assign("info", $info);
        return $this->fetch('billInfo');
    }

    /**
     * 查找当前钱包是否可以扣款
     * @param int $money
     * @param string $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function checkMoneyEnough($money = 0, $id = '')
    {
        $wallet = DB::name('wallet')->where('id', $id)->find();
        if (!checkArr($wallet)) {
            return array('res' => false, 'msg' => '当前钱包余额不足，请先充值');
        }
        if ($wallet['money'] < $money) {
            return array('res' => false, 'msg' => '当前钱包余额不足，请先充值');
        }
        return array('res' => true);
    }


    /******************************* 子对公充值审核 ****************************/

    /**
     * 充值审核列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function auditrechargeList()
    {

        if (isession('type') != 1) {
            $this->error('无权限操作此功能');
        }

        $where['audit'] = 1;
        $where['purpose'] = 2;
        $where['operate'] = 1;
        $where['type'] = 2;
        $where['project'] = 1;
        $where['operate_audit'] = 1;
        $wallet_log = DB::name('wallet_log')->where($where)->paginate(15);
        $list = $wallet_log->all();

        $company = [];
        if (checkArr($list)) {
            $company_ids = array_unique(array_column($list, 'company_id'));
            $company = DB::name('company')->where(['id' => ['in', $company_ids]])->select();
            checkArr($company) && $company = arrayCombine($company, 'id');
        }
        $this->assign('company', $company);
        $this->assign('list', $wallet_log);
        return $this->fetch('auditrechargeList');
    }

    /**
     * 修改钱包审核状态
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function auditrecharge()
    {
        $updateData['audit'] = post('audit');
        $updateData['audit_uid'] = isession('user_id');
        $updateData['audit_name'] = ssession('username');
        $updateData['audit_truename'] = ssession('truename');
        DB::name('wallet_log')->where('id', post('id'))->update($updateData);

        if (post('audit') == 2) {
            //获取当前记录详细信息
            $info = DB::name('wallet_log')->where('id', post('id'))->find();
            $sql = "UPDATE ean_wallet SET money = (money+" . $info['money'] . ") WHERE id =" . $info['wallet_id'];
            Db::execute($sql);
        }
        $msg = post('audit') == 2 ? '审核完成' : '已拒绝';
        $this->success($msg);
    }

    /******************************* 采购审核 *****************************/

    /**
     * 采购审核列表
     */
    public function auditPurchaseList()
    {
        $where['audit'] = 1;
        $where['purpose'] = 1;
        $where['operate'] = 2;
        $where['project'] = 3;
        $where['company_id'] = isession('company_id');
        $where['type'] = isession('type');
        $wallet_log = DB::name('wallet_log')->where($where)->paginate(15);
        $list = $wallet_log->all();

        if (checkArr($list)) {
            $detail_ids = array_unique(array_column($list, 'detail_id'));
            $purchase = DB::name('purchase')->where(['id' => ['in', $detail_ids]])->select();
            if (checkArr($purchase)) {
                $purchase = arrayCombine($purchase, 'id');
                foreach ($list as $key => $value) {
                    if (isset($purchase[$value['detail_id']])) {
                        $list[$key]['title'] = $purchase[$value['detail_id']]['title'];
                        $list[$key]['url'] = $purchase[$value['detail_id']]['url'];
                        //获取产品地址
                        switch ($purchase[$value['detail_id']]['platform']) {
                            case 1://亚马逊
                                $list[$key]['fromUrl'] = $this->makeAmazonUrl($purchase[$value['detail_id']]['shop_id'], $purchase[$value['detail_id']]['asin']);
                                break;
                            case 2://ebay
                                $list[$key]['fromUrl'] = '';
                                break;
                            case 3://速卖通
                                $list[$key]['fromUrl'] = '';
                                break;
                            case 4://wish
                                $list[$key]['fromUrl'] = '';
                                break;
                        }
                    }
                }
            }
        }
        $this->assign('list', $list);
        $this->assign('wallet_log', $wallet_log);
        return $this->fetch('auditPurchaseList');
    }

    /**
     * 修改采购审核状态
     */
    public function auditPurchase()
    {
        $updateData['audit'] = post('audit');
        $updateData['audit_uid'] = isession('user_id');
        $updateData['audit_name'] = ssession('username');
        $updateData['audit_truename'] = ssession('truename');

        $info = DB::name('wallet_log')->where('id', post('id'))->find();
        if (post('audit') == 2) {
            $res = $this->checkMoneyEnough($info['money'], $info['wallet_id']);
            if ($res['res'] == false) {
                $this->error($res['msg']);
            }
            $sql = "UPDATE ean_wallet SET money = (money-" . $info['money'] . ") WHERE id =" . $info['wallet_id'];
            Db::execute($sql);
        }
        DB::name('wallet_log')->where('id', post('id'))->update($updateData);
        Db::name('purchase')->where('id', $info['detail_id'])->update(['audit' => post('audit'), 'wallet_log_id' => post('id')]);
        $msg = post('audit') == 2 ? '审核完成' : '已拒绝';
        $this->success($msg);

    }

    /**
     * 审核采购详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function purchaseInfo($id)
    {
        $info = DB::name('wallet_log')->where('id', $id)->find();

        $purchase = Db::name('purchase')->where('id', $info['detail_id'])->find();

        //获取采购支付账号
        $account = !empty($purchase['account_id']) ? DB::name('pay_account')->where('id', $purchase['account_id'])->find() : [];
        $purchase['account'] = checkArr($account) ? $account['type'] . "-" . $account['account'] : '';
        //获取产品地址
        switch ($purchase['platform']) {
            case 1://亚马逊
                $purchase['fromUrl'] = $this->makeAmazonUrl($purchase['shop_id'], $purchase['asin']);
                break;
            case 2://ebay
                $purchase['fromUrl'] = '';
                break;
            case 3://速卖通
                $purchase['fromUrl'] = '';
                break;
            case 4://wish
                $purchase['fromUrl'] = '';
                break;
        }
        $this->assign('purchase', $purchase);

        $this->assign("info", $info);
        return $this->fetch('purchaseInfo');
    }

    /******************************* 运单审核 ****************************/

    /**
     * 运单审核列表
     */
    public function auditWaybillList()
    {
        if (isession('type') != 1) {
            $this->error('无权限操作此功能');
        }
        $where['audit'] = 1;
        $where['operate'] = 2;
        $where['project'] = 4;
        $where['operate_audit'] = 1;
        $wallet_log = DB::name('wallet_log')->where($where)->paginate(15);
        $list = $wallet_log->all();

        $company = [];
        $product = [];
        if (checkArr($list)) {
            $company_ids = array_unique(array_column($list, 'company_id'));
            $company = DB::name('company')->where(['id' => ['in', $company_ids]])->select();
            checkArr($company) && $company = arrayCombine($company, 'id');

            $detail_ids = array_unique(array_column($list, 'detail_id'));
            //获取运单下有图片url的商品
            $where2['url'] = ['neq', ''];
            $where2['waybill_id'] = ['in', join(',', $detail_ids)];
            $product = Db::name('waybill_product')
                ->field('waybill_id,url')
                ->where($where2)
                ->group('waybill_id')
                ->select();
            checkArr($product) && $product = arrayCombine($product, 'waybill_id');
        }
        $this->assign('company', $company);
        $this->assign('product', $product);
        $this->assign('list', $wallet_log);
        return $this->fetch('auditWaybillList');
    }

    /**
     * 修改运单审核状态
     */
    public function auditWaybill()
    {
        $updateData['audit'] = post('audit');
        $updateData['audit_uid'] = isession('user_id');
        $updateData['audit_name'] = ssession('username');
        $updateData['audit_truename'] = ssession('truename');

        $info = DB::name('wallet_log')->where('id', post('id'))->find();
        if (post('audit') == 2) {
            $res = $this->checkMoneyEnough($info['money'], $info['wallet_id']);
            if ($res['res'] == false) {
                $this->error($res['msg']);
            }
            $sql = "UPDATE ean_wallet SET money = (money-" . $info['money'] . ") WHERE id =" . $info['wallet_id'];
            Db::execute($sql);
        }
        DB::name('wallet_log')->where('id', post('id'))->update($updateData);
        Db::name('waybill')->where('id', $info['detail_id'])->update(['audit' => post('audit'), 'wallet_log_id' => post('id')]);
        $msg = post('audit') == 2 ? '审核完成' : '已拒绝';
        $this->success($msg);
    }

    /**
     * 审核运单详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function waybillInfo($id)
    {
        $info = DB::name('wallet_log')->where('id', $id)->find();

        //运单信息
        $waybill = Db::name('waybill')->where('id', $info['detail_id'])->find();
        //运单下的商品
        $waybillGoods = Db::name('waybill_product')
            ->where('waybill_id', $info['detail_id'])
            ->select();
        //物流公司信息
        $logistics = arrayCombine($this->getLogistics(), 'id');
        $this->assign('logistics', $logistics);
        $this->assign('waybill', $waybill);
        $this->assign('waybillGoods', $waybillGoods);
        $this->assign("info", $info);
        return $this->fetch('waybillInfo');
    }

    /******************************* 财务报表 ****************************/
    public function reportForms()
    {
        $company_id = iget('company_id') ? iget('company_id') : isession('company_id');
        $company = [];
        if (isession('type') == 1) {
            $company = $this->getCompany(isession('company_id'));
            $company = checkArr($company) ? arrayCombine($company, 'id') : [];
        }
        $this->assign("company", $company);

        if (get('start')) {
            $start = strtotime(get('start') . " 00:00:00");
        } else {
            $start = mktime(0, 0, 0, date('m'), 1, date('Y'));
        }
        $this->assign("start", $start);
        if (get('end')) {
            $end = strtotime(get('end') . " 23:59:59");
        } else {
            $end = time();
        }
        $this->assign("end", $end);
        //计算订单总价
        $ordertotal = $this->getOrderProfit($start, $end, $company_id);
        $this->assign("ordertotal", $ordertotal);
        //采购支出
        $pwhere['company_id'] = $company_id;
        $pwhere['operate'] = 2;
        $pwhere['project'] = 3;
        $pwhere['audit'] = 2;
        $pwhere['timestamp'] = ['>=', $start];
        $pwhere1['timestamp'] = ['<=', $end];
        $purchaseExpense = DB::name('wallet_log')->where($pwhere)->where($pwhere1)->sum('money');
        $this->assign("purchaseExpense", $purchaseExpense);
        //运费支出
        $bwhere['company_id'] = $company_id;
        $bwhere['operate'] = 2;
        $bwhere['project'] = 4;
        $bwhere['audit'] = 2;
        $bwhere['timestamp'] = ['>=', $start];
        $bwhere1['timestamp'] = ['<=', $end];
        $waybillExpense = DB::name('wallet_log')->where($bwhere)->where($bwhere1)->sum('money');
        $this->assign("waybillExpense", $waybillExpense);
        //其他支出
        $owhere['company_id'] = $company_id;
        $owhere['operate'] = 2;
        $owhere['project'] = 2;
        $owhere['audit'] = 2;
        $owhere['timestamp'] = ['>=', $start];
        $owhere1['timestamp'] = ['<=', $end];
        $otherExpense = DB::name('wallet_log')->where($owhere)->where($owhere1)->sum('money');
        $this->assign("otherExpense", $otherExpense);
        //自用钱包充值
        $swhere['company_id'] = $company_id;
        $swhere['purpose'] = 1;
        $swhere['operate'] = 1;
        $swhere['project'] = 1;
        $swhere['audit'] = 2;
        $swhere['timestamp'] = ['>=', $start];
        $swhere1['timestamp'] = ['<=', $end];
        $selfRecharge = DB::name('wallet_log')->where($swhere)->where($swhere1)->sum('money');
        $this->assign("selfRecharge", $selfRecharge);
        //对公钱包充值
        if(isession('type') == 2 || iget('company_id')!=''){
            $lwhere['company_id'] = $company_id;
            $lwhere['purpose'] = 2;
            $lwhere['operate'] = 1;
            $lwhere['project'] = 1;
            $lwhere['audit'] = 2;
            $lwhere['timestamp'] = ['>=', $start];
            $lwhere1['timestamp'] = ['<=', $end];
            $publicRecharge = DB::name('wallet_log')->where($lwhere)->where($lwhere1)->sum('money');
            $this->assign("publicRecharge", $publicRecharge);
            return $this->fetch('subsidiaryReportForms');
        }
        return $this->fetch('corporationReportForms');
    }


}